#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>

#define D_MAP_FILE_PATH "/agvwork/AgvFundation/fundation/data/robotData1/map/"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pathRoute=new PathRouteCalculate;
    mapManager= MapManager::getInstance();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    // 新建图
    Graph<string> G;
    string temp[]= { "v0","v1","v2","v3","v4" };
    /*int length = sizeof(temp) / sizeof(temp[0]);
    G.vertexNum = length;
    G.arcNum = 7;*/
    ifstream in("/agvwork/AgvFundation/fundation/src/ComponetTest/PathPlanTest/input.txt");
    in >> G.vertexNum >> G.arcNum;
    // 初始化图的顶点信息
    for (int i = 0; i < G.vertexNum; i++)
    {
        G.vertex[i] = temp[i];
    }
    //初始化图G的边权值
    for (int i =0; i <G.vertexNum; i++)
    {
        for (int j = 0; j <G.vertexNum; j++)
        {
            G.arc[i][j] = MAXCOST;
        }
    }
    for (int i = 0; i < G.arcNum; i++)
    {
        int m, n,cost;
        in >> m >> n >> cost;
        G.arc[m][n] = cost;
    }
    pathRoute->Dijkstra(G, 0);

    qDebug()<<"finished";

}

void MainWindow::on_pushButton_loadMap_clicked()
{
    //定义文件对话框类
   QFileDialog *fileDialog = new QFileDialog(this);
   fileDialog->resize(400,500);
   //定义文件对话框标题
   fileDialog->setWindowTitle(tr("选择文件"));
   //设置默认文件路径
   QString configFile=D_MAP_FILE_PATH;

   fileDialog->setDirectory(configFile);
   //设置文件过滤器
   fileDialog->setNameFilter(tr("json(*.mjson)"));
   //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
   fileDialog->setFileMode(QFileDialog::ExistingFiles);
   //设置视图模式
   fileDialog->setViewMode(QFileDialog::Detail);
   //打印所有选择的文件的路径
   QStringList fileNames;
   if(fileDialog->exec())
   {
       fileNames = fileDialog->selectedFiles();
   }
    qDebug()<<fileNames;
    if(fileNames.size()>0)
    {

        QString filePath =fileNames[0];
        mapManager->loadMap( filePath.toStdString());

    }
}

void MainWindow::on_pushButton_setStationOccupy_clicked()
{
    int agvid=ui->lineEdit_agvid_occupy->text().toInt();
    int stationId=ui->lineEdit_stationId_occupy->text().toInt();
    mapManager->addOccuStation(stationId,agvid);
    qDebug()<<"addOccuStation"<<agvid<<stationId;
}

void MainWindow::on_pushButton_releaseStationOccupy_clicked()
{
    int agvid=ui->lineEdit_agvid_occupy->text().toInt();
    int stationId=ui->lineEdit_stationId_occupy->text().toInt();
    mapManager->freeStation(stationId,agvid);
    qDebug()<<"freeStation"<<agvid<<stationId;
}

void MainWindow::on_pushButton_calculatePathList_clicked()
{
    int agvid=ui->lineEdit_getPath_agvid->text().toInt();
    int lastStationId=ui->lineEdit_getPath_lastStation->text().toInt();
    int startStationId=ui->lineEdit_getPath_startStation->text().toInt();
    int endStationId=ui->lineEdit_getPath_endStation->text().toInt();
    bool isReverse=ui->comboBox_isReverse->currentIndex();
    int tmpLength;
//    std::vector<int> tmpPathList=mapManager->getBestPath(agvid,lastStationId, startStationId, endStationId,
//                                 tmpLength, isReverse);
    std::vector<int> tmpPathList=mapManager->getPath_byStation(startStationId, endStationId,
                                 tmpLength, isReverse);
    ui->textEdit_pathList->clear();
    if(0==tmpPathList.size())
    {
        ui->textEdit_pathList->append("no path");
    }
    else
    {
        QString tmpStr="length";
        tmpStr+=QString::number(tmpLength);
        ui->textEdit_pathList->append(tmpStr);
        for(int i=0;i<tmpPathList.size();i++)
        {
            MapPath *tmpPath=mapManager->getPathById(tmpPathList[i]);
            QString pathInfo="path ";
            pathInfo+=QString::number(tmpPathList[i]);
            pathInfo+=" ,stationId"+QString::number(tmpPath->getEnd());
            ui->textEdit_pathList->append(pathInfo);
        }
    }
}

void MainWindow::on_pushButton_setPathOccupy_clicked()
{
    int agvid=ui->lineEdit_agvid_occupy_2->text().toInt();
    int stationId=ui->lineEdit_pathId_occupy->text().toInt();
    mapManager->addOccuLine(stationId,agvid);
    qDebug()<<"addOccuLine"<<agvid<<stationId;
}

void MainWindow::on_pushButton_releasePathOccupy_clicked()
{
    int agvid=ui->lineEdit_agvid_occupy_2->text().toInt();
    int stationId=ui->lineEdit_pathId_occupy->text().toInt();
    mapManager->freeLine(stationId,agvid);
    qDebug()<<"freeLine"<<agvid<<stationId;
}
